package com.open.party.cloud.view.examine.examineForGanbu.adpater;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.ExamineBean;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperListener;

import java.util.ArrayList;
import java.util.List;

public class ExamineForMoNiSjListAdapter extends RecyclerView.Adapter<ExamineForMoNiSjListAdapter.ViewHolder> {

    // 展示数据
    protected ArrayList<ExamineBean> mData;
    private Context mContext;
    private OnSuperListener<ExamineBean> onSuperListener;

    public ExamineForMoNiSjListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<ExamineBean> onSuperListener) {
        this.onSuperListener = onSuperListener;
    }

    public void setData(List<ExamineBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<ExamineBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.examine_gb_sj_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final ExamineBean itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getName()));

        holder.examineTimeTv.setText("时长: " + itemData.getLengthOfExamination() + " 分钟");
        holder.integralTv.setText("最好成绩: " + itemData.getScore());

        // -1 缺考，1，未开始，2，正在考试
        // 如果没有考，就有三种情况，未开始，考试中，已结束  已考
        holder.scoreTv.setVisibility(View.GONE);
//        if (itemData.isTest()) {
//            holder.stateTv.setText("已考");
//            holder.stateTv.setTextColor(ContextCompat.getColor(mContext, R.color.app2_green));
//            holder.scoreTv.setVisibility(View.VISIBLE);
//            holder.scoreTv.setText(itemData.getScore() + "分");
//        } else {

//        }

        if ("未开始".equals(itemData.getExaminationState())) {
            holder.stateTv.setText("未开始");
            holder.stateTv.setTextColor(ContextCompat.getColor(mContext, R.color.app_orange));

        } else if ("考试中".equals(itemData.getExaminationState())) {
            holder.stateTv.setText("考试中");
            holder.stateTv.setTextColor(ContextCompat.getColor(mContext, R.color.app_green));
        } else {
            holder.stateTv.setText(itemData.getExaminationState());
            holder.stateTv.setTextColor(ContextCompat.getColor(mContext, R.color.app_red));
        }

        String timeStartStr = XUtils.date().getDateStrByDate(itemData.getStartTime(), "yyyy-MM-dd HH:mm");
        String timeEndStr = XUtils.date().getDateStrByDate(itemData.getEndTime(), "yyyy-MM-dd HH:mm");
        holder.timeTv.setText("考试时间：" + timeStartStr + " - " + timeEndStr);

//        if ("ZC".equals(itemData.getState())) {
//            holder.stateTv.setText("正常");
//            holder.stateTv.setTextColor(Color.parseColor("#14F83A"));
//        } else if ("YJY".equals(itemData.getState())) {
//            holder.stateTv.setText(":已结业");
//            holder.stateTv.setTextColor(Color.parseColor("#F22119"));
//        }else{
//            holder.stateTv.setText("未知状态");
//            holder.stateTv.setTextColor(Color.parseColor("#666666"));
//        }
//        holder.stateTv.setText(XUtils.string().getNotNullValue(itemData.getExaminationState(),"未知状态"));

        if (mData.size() - 1 == position) {
            holder.bottomLineTv.setVisibility(View.VISIBLE);
        } else {
            holder.bottomLineTv.setVisibility(View.GONE);
        }

        if (onSuperListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSuperListener.onClick(position, itemData, "rootView");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView titleTv, examineTimeTv, integralTv, timeTv, stateTv, scoreTv, bottomLineTv;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            examineTimeTv = itemView.findViewById(R.id.examineTimeTv);
            integralTv = itemView.findViewById(R.id.integralTv);
            timeTv = itemView.findViewById(R.id.timeTv);
            stateTv = itemView.findViewById(R.id.stateTv);
            scoreTv = itemView.findViewById(R.id.scoreTv);

            bottomLineTv = itemView.findViewById(R.id.bottomLineTv);
        }
    }
}